import { _decorator, Component, Node, Widget } from 'cc';
import { AudioManger } from './AudioManger';
import { Clips } from './Enums';
import { AdManger } from './ad/AdManger';
const { ccclass, property } = _decorator;

@ccclass('BasePage')
export class BasePage extends Component {

    /** 页面初始隐藏位置（Y轴） */
    private static readonly INIT_Y = -2200;

    /** 页面打开时的位置 */
    private static readonly CENTER_POS = { x: 0, y: 0 };

    /** 页面关闭时移出的位置（X轴） */
    private static readonly CLOSE_X = -1000;

    start() {
        // 页面初始位置设置为隐藏状态
        this.node.setPosition(BasePage.CENTER_POS.x, BasePage.INIT_Y);
    }

    update(deltaTime: number) {}

    /**
     * 打开页面：激活节点、居中、适配 Widget、展示广告
     */
    open() {
        this.node.active = true;
        this.node.setPosition(BasePage.CENTER_POS.x, BasePage.CENTER_POS.y);
        this._fitToFullScreen();
        AdManger.show_banner();
    }

    /**
     * 关闭页面：播放音效、隐藏节点、关闭广告
     */
    close() {
        AudioManger.instance.playSound(Clips.btn_1);
        this.node.setPosition(BasePage.CLOSE_X, 0);
        this.node.active = false;
        AdManger.close_ad();
    }

    /**
     * 内部方法：将节点适配为全屏
     */
    private _fitToFullScreen() {
        const widget = this.node.getComponent(Widget);
        if (widget) {
            widget.top = 0;
            widget.bottom = 0;
            widget.left = 0;
            widget.right = 0;
        }
    }
}
